shell 练习-找出活动ip

写一个脚本,实现判断192.168.0.0网络里,当前在线的IP有哪些

思路: for循环, 0.1 – 0.254 依次去ping,能通说明在线。

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
ips="192.168.0."
for i in `seq 1 254`
do
ping -c 2 $ips$i >/dev/null 2>/dev/null
if [ $? == 0 ]
then
echo "echo $ips$i is online"
else
echo "echo $ips$i is not online"
fi
done

也可以使用 nmap 命令,速度比较快一点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
myyum () {
if ! rpm -qa|grep -q "^$1"
then
yum install -y "^$1"
else
echo $1 already installed.
fi
}
for i in nmap
do
myyum $i
done
nmap -sP 192.168.0.0/24